#pragma once
#include<string>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<cerrno>
#include<cassert>
#include<unistd.h>
#include<fcntl.h>
#include<iostream>
using namespace std;

#define NAMED_PIPE "/tmp/cxs.named_pipe"

bool createFifo(const string& path)
{
    
    
    int n = mkfifo(path.c_str(), 0666);
    
    if(n == 0) 
        return true;
    
    // 命名管道已存在，可以认为是成功的
    else if(errno == EEXIST) 
        return true;
    
    else 
    {
        cout<< "Failed to create fifo " << path << ": " << strerror(errno) << endl;
        return false;
    }
}

bool removeFifo(const string& path)
{
    int n = unlink(path.c_str());
    if(n == -1 && errno != ENOENT) 
    { 
        // ENOENT表示文件不存在，不算错误
        cout << "Failed to remove fifo " << path << ": " << strerror(errno) << endl;
        return false;
    }
    return true;
}